﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WROA.Models.Entitys;
using WROA.Models.Services;
using System.IO;
using WROA.Models.IServices;

namespace WROA.Controllers
{
    public class DocumentController : Controller
    {
        //
        // GET: /Document/

        /// <summary>
        /// 申请公文
        /// </summary>
        public ActionResult ApplyDocument()
        {
            ApplyDocumentView model = new ApplyDocumentView();
            return View(model);
        }

        /// <summary>
        /// 申请公文Post
        /// </summary>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult ApplyDocument(FormCollection form, HttpPostedFileBase Uploadfile)
        {
            IUserService us = new UserService();
            IFlowService fs = new FlowService();
            IDocumentService ds = new DocumentService();

            ApplyDocument applyDocument = new ApplyDocument();
            applyDocument.Ad_time = DateTime.Now;
            applyDocument.User = us.GetUser(User.Identity.Name);
            applyDocument.Flow = fs.GetFlow(form["FlowName"]);//根据流程名得到Flow对象
            applyDocument.Procedure = fs.GetFirstProcedure(applyDocument.Flow);//获取流程第一个步骤
            applyDocument.Ad_name = form["DocumentName"];
            applyDocument.Ad_remark = form["DocumentRemark"];
            applyDocument.Ad_htmldata = form["Html"];
            applyDocument.Ad_state = "未结束";
            applyDocument.Ad_isagree = null;
            //applyDocument.U_name = null;
            if (Uploadfile != null)//如果有上传附件
            {
                Uploadfile.SaveAs(Server.MapPath("~/UpLoadFile/DocumentFile/") + Uploadfile.FileName);//保存到服务器
                //保存到数据库
                Models.Entitys.File file = new Models.Entitys.File();
                file.User = us.GetUser(User.Identity.Name);
                file.File_name = Uploadfile.FileName;
                file.File_size = Uploadfile.ContentLength.ToString();
                file.File_date = DateTime.Now;
                file.File_path = Server.MapPath("~/UpLoadFile/DocumentFile/");
                fs.Save(file);

                applyDocument.File = file;
            }
            else//如果没有上传附件
            {
                applyDocument.File = null;
            }
            ds.Save(applyDocument);

            return View();
        }


    }
}
